#include "healthassessment.h"
#include "messagetype.h"
#include <QMessageBox>
#include <QDebug>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPalette>
#include <QColor>

HealthAssessment::HealthAssessment(QTcpSocket *socket, QWidget *parent)
    : QWidget(parent), tcpSocket(socket) {
    // Set window title and fixed size
    setWindowTitle("健康评估");
    setFixedSize(600, 600); // 增大窗口大小

    // Set background color
    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, QColor(248, 248, 255));
    this->setPalette(palette);
    this->setAutoFillBackground(true);

    // Main layout
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, 20, 20, 20);
    mainLayout->setSpacing(15);

    // Title label
    QLabel *label = new QLabel("<h2>健康评估</h2>", this);
    label->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(label);

    // Form layout for patient information
    QFormLayout *formLayout = new QFormLayout();
    formLayout->setSpacing(10);

    // Create and style input fields
    patientidEdit = new QLineEdit(this);
    patientidEdit->setPlaceholderText("请输入患者ID");
    setInputFieldStyle(patientidEdit);

    patientnameEdit = new QLineEdit(this);
    patientnameEdit->setPlaceholderText("请输入患者姓名");
    setInputFieldStyle(patientnameEdit);

    heightEdit = new QLineEdit(this);
    heightEdit->setPlaceholderText("身高(cm)");
    setInputFieldStyle(heightEdit);

    weightEdit = new QLineEdit(this);
    weightEdit->setPlaceholderText("体重(kg)");
    setInputFieldStyle(weightEdit);

    bloodPressureEdit = new QLineEdit(this);
    bloodPressureEdit->setPlaceholderText("血压(mmHg)");
    setInputFieldStyle(bloodPressureEdit);

    heartRateEdit = new QLineEdit(this);
    heartRateEdit->setPlaceholderText("心率(bpm)");
    setInputFieldStyle(heartRateEdit);

    // Add fields to form layout
    formLayout->addRow("患者ID", patientidEdit);
    formLayout->addRow("患者姓名", patientnameEdit);
    formLayout->addRow("身高(cm)", heightEdit);
    formLayout->addRow("体重(kg)", weightEdit);
    formLayout->addRow("血压(mmHg)", bloodPressureEdit);
    formLayout->addRow("心率(bpm)", heartRateEdit);

    mainLayout->addLayout(formLayout);

    // Button layout
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    QPushButton *saveButton = createStyledButton("保存", "#4CAF50");
    QPushButton *backButton = createStyledButton("返回", "#f44336");

    // Add spacing around buttons for a cleaner look
    buttonLayout->addStretch(); // Add stretchable space
    buttonLayout->addWidget(saveButton);
    buttonLayout->addWidget(backButton);
    buttonLayout->addStretch(); // Add stretchable space

    mainLayout->addSpacing(20);
    mainLayout->addLayout(buttonLayout);

    // Set main layout
    setLayout(mainLayout);

    // Connect buttons to slots
    connect(saveButton, &QPushButton::clicked, this, &HealthAssessment::onSaveButtonClicked);
    connect(backButton, &QPushButton::clicked, this, &HealthAssessment::onBackButtonClicked);
}

// Function to style input fields
void HealthAssessment::setInputFieldStyle(QLineEdit *lineEdit) {
    lineEdit->setStyleSheet("padding: 8px; border: 1px solid #ccc; border-radius: 5px;");
    lineEdit->setFixedHeight(35);
}

// Function to create styled buttons
QPushButton* HealthAssessment::createStyledButton(const QString &text, const QString &color) {
    QPushButton *button = new QPushButton(text, this);
    button->setStyleSheet(QString("background-color: %1; color: white; border: none; border-radius: 5px; padding: 10px;").arg(color));
    button->setFixedHeight(40);
    return button;
}

void HealthAssessment::onSaveButtonClicked() {
    if (patientidEdit->text().isEmpty()) {
        QMessageBox::warning(this, "输入错误", "患者ID是必填项，请填写");
        return;
    }

    // Collect assessment data and send to server
    QMap<QString, QString> assessmentData;
    assessmentData.insert("type", HEALTHASSESSMENTDATABASE);
    assessmentData.insert("databaseoperator", QString::number(INSERT));
    assessmentData.insert("patientid", patientidEdit->text());
    assessmentData.insert("patientname", patientnameEdit->text());
    assessmentData.insert("height", heightEdit->text());
    assessmentData.insert("weight", weightEdit->text());
    assessmentData.insert("bloodpressure", bloodPressureEdit->text());
    assessmentData.insert("heartrate", heartRateEdit->text());
    QByteArray data = mapToByteArrayCustom(assessmentData);

    // Check connection and send data
    if (tcpSocket->state() == QAbstractSocket::UnconnectedState) {
        tcpSocket->connectToHost("127.0.0.1", 12345);
    }
    if (tcpSocket->waitForConnected(5000)) {
        tcpSocket->write(data);
        tcpSocket->flush();
    }
}

void HealthAssessment::onBackButtonClicked() {
    this->close();
}
